from django.conf import settings
from django.shortcuts import render

# Create your views here.


from rest_framework.viewsets import GenericViewSet

from home.serializer import BannerSerializer
from utils.common_mixin import CommonListModelMixin as ListModelMixin
from utils.common_logger import logger
from home import models
from rest_framework.views import APIView
from rest_framework.exceptions import ValidationError
from django.core.cache import cache

from utils.common_response import APIResponse
from utils.my_print import new_print


class BannerView(GenericViewSet, ListModelMixin):
    """
    轮播图
    """

    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('-id')[:settings.BANNER_NUM]
    serializer_class = BannerSerializer

    def list(self, request, *args, **kwargs):
        response = super().list(request, *args, **kwargs)
        # print("我走了")
        # 进行轮播图缓存
        data = cache.get('home_banner_list')
        if not data:
            # 进行数据缓存
            data = response.data.get('data')  # {code:100,msg:成功，data:[{},{}]}
            cache.set('home_banner_list', data)
        return APIResponse(data=data)


class t1(APIView):
    def get(self, request):
        raise ValidationError('llll')
